package com.ruoyi.common.vip.service.impl;

import com.ruoyi.common.vip.entity.YytDeliveryDate;
import com.ruoyi.common.vip.dao.YytDeliveryDateMapper;
import com.ruoyi.common.vip.service.YytDeliveryDateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* <p>
    *  服务实现类
    * </p>
*
* @author MrPing
* @since 2022-07-18
*/
@Service
public class YytDeliveryDateServiceImpl extends ServiceImpl<YytDeliveryDateMapper, YytDeliveryDate> implements YytDeliveryDateService {

    @Autowired
    private YytDeliveryDateMapper yytDeliveryDateMapper;

    @Override
    public List<String> getDeliverDate() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        // 获取当前日期
        String newDate = String.valueOf(LocalDate.now());
        calendar.setTime(dateFormat.parse(newDate));

        int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);

        if(dayWeek == 1){
            // 获取下周数据
            calendar.add(Calendar.DAY_OF_MONTH, 7);
            String endDate = dateFormat.format(calendar.getTime());
            return  yytDeliveryDateMapper.getVipSendDateList(newDate, endDate);
        }else{
            // 获取本周数据
            calendar.add(Calendar.DAY_OF_MONTH, 1-dayWeek+7);
            String endDate = dateFormat.format(calendar.getTime());
            List<String> sendDateList = yytDeliveryDateMapper.getVipSendDateList(newDate, endDate);
            if(sendDateList.size() == 0){
                calendar.add(Calendar.DAY_OF_MONTH, 1-dayWeek+14);
                endDate = dateFormat.format(calendar.getTime());
                return yytDeliveryDateMapper.getVipSendDateList(newDate, endDate);
            }
            return sendDateList;
        }

    }
}
